package com.xbongbong.pro.rabbitmq.producer.impl;

import com.xbongbong.paas.pojo.dto.ImportMessageFormDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.rabbitmq.binders.ImportProducerBinder;
import com.xbongbong.pro.rabbitmq.producer.ImportProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 梁鲁江
 * @version v1.0
 * @date 2019/9/18 16:29
 * @since v1.0
 */
@Service("importProducer")
@EnableBinding({ImportProducerBinder.class})
public class ImportProducerImpl implements ImportProducer {
    private static final Logger LOG = LoggerFactory.getLogger(ImportProducerImpl.class);
    @Resource
    private ImportProducerBinder importProducerBinder;
    @Override
    public void sendMessage(ImportMessageFormDataDTO importMessageFormDataDTO) throws XbbException {
        MessageBuilder<ImportMessageFormDataDTO> messageBuilder = MessageBuilder.withPayload(importMessageFormDataDTO);;
        Message<ImportMessageFormDataDTO> message = messageBuilder.build();
        boolean send = importProducerBinder.importAdd().send(message);
        if(!send){
            LOG.error("导入消息发送失败");
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public void sendLargeNumMessage(ImportMessageFormDataDTO importMessageFormDataDTO) throws XbbException {
        MessageBuilder<ImportMessageFormDataDTO> messageBuilder = MessageBuilder.withPayload(importMessageFormDataDTO);;
        Message<ImportMessageFormDataDTO> message = messageBuilder.build();
        boolean send = importProducerBinder.importLargeNumAdd().send(message);
        if(!send){
            LOG.error("导入消息发送失败");
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }
}
